<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        input:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 5px rgba(74, 144, 226, 0.3);
        }
        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #357ab8;
        }
        .input-error {
            border-color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <form id="loginForm" onsubmit="return validateForm()">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名">
                <div id="usernameError" class="error-message">用户名不能为空且长度在4-16个字符之间</div>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码">
                <div id="passwordError" class="error-message">密码不能为空且长度在6-20个字符之间</div>
            </div>
            <button type="submit">登录</button>
        </form>
    </div>

    <script>
        // 实时验证函数
        function validateInput(inputElement, errorElement, validationFn, errorMessage) {
            inputElement.addEventListener('blur', function() {
                if (!validationFn(this.value)) {
                    this.classList.add('input-error');
                    errorElement.textContent = errorMessage;
                    errorElement.style.display = 'block';
                } else {
                    this.classList.remove('input-error');
                    errorElement.style.display = 'none';
                }
            });

            // 输入时尝试清除错误状态
            inputElement.addEventListener('input', function() {
                if (validationFn(this.value)) {
                    this.classList.remove('input-error');
                    errorElement.style.display = 'none';
                }
            });
        }

        // 验证用户名
        const username = document.getElementById('username');
        const usernameError = document.getElementById('usernameError');
        validateInput(username, usernameError, function(value) {
            return value.trim().length >= 4 && value.trim().length <= 16;
        }, '用户名长度必须在4-16个字符之间');

        // 验证密码
        const password = document.getElementById('password');
        const passwordError = document.getElementById('passwordError');
        validateInput(password, passwordError, function(value) {
            return value.trim().length >= 6 && value.trim().length <= 20;
        }, '密码长度必须在6-20个字符之间');

        // 表单提交验证
        function validateForm() {
            let isValid = true;

            // 验证用户名
            if (username.value.trim().length < 4 || username.value.trim().length > 16) {
                username.classList.add('input-error');
                usernameError.style.display = 'block';
                isValid = false;
            }

            // 验证密码
            if (password.value.trim().length < 6 || password.value.trim().length > 20) {
                password.classList.add('input-error');
                passwordError.style.display = 'block';
                isValid = false;
            }

            if (isValid) {
                // 这里可以添加实际的表单提交逻辑
                alert('表单验证通过，即将提交...');
                // document.getElementById('loginForm').submit();
                // 实际使用时请取消上面的注释，并移除下面的return false
                return false; // 仅用于演示，阻止表单实际提交
            }

            return false; // 阻止表单提交
        }
    </script>
</body>
</html>